package com.vonchenchen.usbmuxd_android;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.fragment.app.FragmentActivity;
import com.vonchenchen.usbmuxd_android.usbmuxd.muxdprotocol.DataEngine;


public class MainActivity extends FragmentActivity {

    private static final String  TAG = "MainActivity";

    private Button mStartBtn;
    private Button mStopBtn;
    private Button mTestActivityBtn;

    private DataEngine _usbClient;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _usbClient=new DataEngine(this);

        mStartBtn = (Button) findViewById(R.id.btn_start);
        mStopBtn = (Button) findViewById(R.id.btn_stop);
        mTestActivityBtn = (Button) findViewById(R.id.btn_testActivity);
        _usbClient.start();
        mTestActivityBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
                startActivity(intent);
            }
        });
        mStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _usbClient.openUSBAsync(true);
            }
        });
        mStopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _usbClient.closeUSB();
            }
        });
        _usbClient.registerListener(new DataEngine.Listener() {
            private boolean _running=false;
            private Thread readtask=new Thread(new Runnable() {
                byte[] readBuffer =new byte[1024*128];
                @Override
                public void run() {
                    long total = 0;
                    long total_bytes=0;
                    while(_running) {
                        int len = _usbClient.readTcpData(readBuffer);
                        total+=len;
                        total_bytes+=len;
//                        Log.d(TAG, "total:" + total);
                        if(total>1024*1024) {
                            Log.d(TAG, "len:" + total_bytes);
                            total = 0;
//                            _usbClient.writeRawData("Hello JYZ".getBytes());
                        }
                        if(len<=0){
                            Log.d(TAG,"len:"+len);
                        }
                    }
                }
            });
            private Thread writetask=new Thread(new Runnable() {
                @Override
                public void run() {
                    while(_running) {
                        int len = _usbClient.writeRawData("Hello JYZ;".getBytes());
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            });
            @Override
            public void onConnected() {
                _running=true;
                readtask.start();
                writetask.start();
            }

            @Override
            public void onDisconnected() {
                _running=false;
                readtask.interrupt();
                writetask.interrupt();
            }
        });
    }

}
